//
//  B_AddressSearchView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/6.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_AddressSearchView.h"

@interface B_AddressSearchView()<UISearchBarDelegate>
@property (nonatomic, strong) UIView * backView;

@end

@implementation B_AddressSearchView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor color_main_background_color];
        [self setSearchView];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 4;
    }
    return self;
}

- (void) setSearchView {
    
    _searchBar = [[UISearchBar alloc] init];
    [self addSubview:_searchBar];
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.left.equalTo(self.mas_left).offset(5);
        make.right.equalTo(self.mas_right).offset(-5);
    }];
    UIImage * searchBarBg = [[UIImage alloc] GetImageWithColor:[UIColor color_main_background_color] alp:1 andHeight:38];
    //设置背景图片
    [_searchBar setBackgroundImage:searchBarBg];
    _searchBar.layer.masksToBounds = YES;
    _searchBar.layer.cornerRadius = 2;
    //设置文本框背景
    [_searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
    [_backView addSubview:_searchBar];
    /// 修改字体颜色
    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    searchField.textColor =  [UIColor color_text_light_gray];
    searchField.font = xgSystemFont(15);
    [searchField setValue: xgTextGlodColor forKeyPath:@"_placeholderLabel.textColor"];
    searchField.placeholder = @"搜索地址";
    /// 修改放大镜
    UIImage *image = [UIImage imageNamed:@"B_topic_home_search"];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
    iconView.frame = CGRectMake(0, 0, image.size.width + 7, image.size.height);
    iconView.contentMode = UIViewContentModeLeft;
    iconView.layer.masksToBounds = YES;
    searchField.leftView = iconView;
    /// 修改光标颜色
    _searchBar.tintColor = [UIColor color_text_light_gray];;
    _searchBar.delegate = self;
}

/**
 TODO:搜索按钮的点击事件
 
 @param searchBar 搜索searchBar
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    if (searchBar.text.length == 0 || ISNULL(searchBar.text)) {
        [UIView xg_toast:@"请输入搜索内容!"];
        return ;
    }
    /// 收回键盘,在这里执行搜索
    [searchBar resignFirstResponder];
    if (self.searchAction) {
        self.searchAction(self.searchBar.text, YES);
    }
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (self.searchAction) {
        self.searchAction(searchText, NO);
    }
}


@end
